package com.example.agrigreennewssys.controller;



import com.example.agrigreennewssys.dto.ArticleDetailDTO;
import com.example.agrigreennewssys.entity.Article;
import com.example.agrigreennewssys.lucene.LuceneIndexService;
import com.example.agrigreennewssys.lucene.LuceneSearchService;
import com.example.agrigreennewssys.service.ArticleService;
import com.example.agrigreennewssys.utils.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.io.IOException;
import java.util.List;

/**
 * @author 光的代言人
 */
@RestController
@RequestMapping("/api/search")
public class LuceneController {

    @Autowired
    private LuceneSearchService luceneSearchService;
    @Autowired
    private LuceneIndexService luceneIndexService;
    @Autowired
    private ArticleService articleService;
    // 在文章保存逻辑中调用
    @GetMapping("/create")
//    @ResponseBody
    public void saveArticle() throws IOException {
        // 查询所有文章并重建索引
//        luceneIndexService.createIndex(allArticles);
        List<ArticleDetailDTO> homeArticles = articleService.getArticleDetails(null,null);
        luceneIndexService.createIndex(homeArticles);
    }
    // 搜索接口
    @GetMapping("/query")
    public Result searchWithHighlight(@RequestParam String query, Model model) throws Exception {
        // 返回带有高亮的搜索结果
//      List<Article> list= luceneSearchService.search(query);
        List<ArticleDetailDTO> list= luceneSearchService.search(query);
//        model.addAttribute("list", list);
        return Result.success(list);
    }
}
